package com.shw.pclient.adapter;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

/**
 * 
 * @author HAHA
 * 
 */
public class SHWUpLoadImage implements Runnable {

//	private static String NAMESPACE = "http://192.168.0.100/mys/services/ImageWS";
//	private static String serviceURL = "http://192.168.0.100/mys/services/ImageWS?wsdl";
	private static String NAMESPACE = "http://192.168.0.100/soap/sayHello";
	private static String serviceURL = "http://192.168.0.100/soap/soapInfo.php?wsdl";
	public static ExecutorService executorService=Executors.newFixedThreadPool(2);
	private byte[] bytes;
	
	public SHWUpLoadImage(byte[] bytes){
		this.bytes=bytes;
	}
	
	public void uploadImage() {
		String methodname = "IfServices";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodname);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.bodyOut = soapObject;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(soapObject);
//		PropertyInfo propertyInfo = new PropertyInfo();
//		
//		propertyInfo.setName("bbytes");
//		propertyInfo.setName("filename");
//		String strByte = Base64.encode(bytes);
//		
//		propertyInfo.setValue(strByte);
//		propertyInfo.setValue("filename");
//		soapObject.addProperty(propertyInfo);
		String strByte = Base64.encode(bytes);
		soapObject.addProperty("filename", "filename");
		soapObject.addProperty("bytes", strByte);
		(new MarshalBase64()).register(envelope);
		HttpTransportSE transport = new HttpTransportSE(serviceURL);
		transport.debug = true;
		try {
			transport.call(null, envelope);
			/*
			 * if(envelope.getResponse()!=null){ SoapObject object =(SoapObject)
			 * envelope.bodyIn; String
			 * result=object.getProperty("out").toString();
			 * if(result!=null&&result.length()>0){ return result; } }
			 */
//			Object a = (Object) envelope.getResponse();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		uploadImage();
	}
}
